﻿using System.Data.Objects;
using SecurityApi.DataAccess;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SecurityApi.Business
{
    public class RoleManager
    {
        private RoleRepository _repository = new RoleRepository();
        private UserRoleRepository _userRoleRepository = new UserRoleRepository();
        public List<SecurityApi.Model.Role> GetApplicationRoles(int applicationId)
        {
            List<SecurityApi.Model.Role> list = new List<Model.Role>();
            List<SecurityApi.DataAccess.Role> efRoles = _repository.GetApplicationRoles(applicationId).ToList();
            foreach (var efRole in efRoles)
            {
                SecurityApi.Model.Role role = new Model.Role(efRole.RoleId, efRole.ApplicationId, efRole.RoleName);
                list.Add(role);
            }

            return list;
        }


        public SecurityApi.Model.Role GetRoleById(int id)
        {
            Role efRole = _repository.GetRoleById(id);

            SecurityApi.Model.Role role = new Model.Role(efRole.RoleId, efRole.ApplicationId, efRole.RoleName);
            return role;
        }

        public List<SecurityApi.Model.Role> GetUserRoles(int userId, int applicationId)
        {
            var query = _userRoleRepository.GetUserRoles(userId,applicationId);

            List<SecurityApi.Model.UserRole> userRoles = query.ToList();
            List<SecurityApi.Model.Role> list = new List<SecurityApi.Model.Role>();
            foreach (var efRole in userRoles)
            {
                SecurityApi.Model.Role role = new Model.Role(efRole.RoleId, efRole.ApplicationId, efRole.RoleName);
                list.Add(role);
            }

            return list;
        }

        public List<SecurityApi.Model.Role> GetUserRoles(string userNme, int applicationId)
        {
            var query = _userRoleRepository.GetUserRoles(userNme, applicationId);

            List<SecurityApi.Model.UserRole> userRoles = query.ToList();
            List<SecurityApi.Model.Role> list = new List<SecurityApi.Model.Role>();
            foreach (var efRole in userRoles)
            {
                SecurityApi.Model.Role role = new Model.Role(efRole.RoleId, efRole.ApplicationId, efRole.RoleName);
                list.Add(role);
            }

            return list;
        }

    }
}
